package com.oocl.aeat.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.aeat.dto.JsonResult;
import com.oocl.aeat.service.VerifyService;
import com.oocl.aeat.service.factory.VerifyServiceFactory;
import com.oocl.aeat.util.Const;
import com.oocl.aeat.util.JsonUtil;

@WebServlet("/sec/check")
public class CheckServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private VerifyService verifyService;
    public CheckServlet() {
    	verifyService = VerifyServiceFactory.getInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String initTime = request.getParameter("initTime");
		response.setContentType("application/json; charset=utf-8");
		if(initTime!=null){
			try{
				int counter = verifyService.countNewVerify(Long.parseLong(initTime));
				JsonResult result = new JsonResult(Const.JSON_SUCCESS,"success",counter);
				out.write(JsonUtil.getJsonFromObject(result));
				out.flush();
				return;
			}catch(Exception e){
				JsonResult result = new JsonResult(Const.JSON_FAIL,"init time not long.",null);
				out.write(JsonUtil.getJsonFromObject(result));
				out.flush();
				return;
			}
		}
	}

}
